package com.example.smartcity.ui.SmartCommynity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.DelivaryMessageBean;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class GetCode extends AppCompatActivity {

    private TextView TVGetPosition;
    private TextView TVCode;
    private TextView TVTel;
    private TextView TVGetTime;
    private TextView TVGetCode;
    private TextView TVOk;
    private Calendar calendar = Calendar.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_code);
        TVGetPosition = findViewById(R.id.TV_GetPosition);
        TVCode = findViewById(R.id.TV_code);
        TVTel = findViewById(R.id.TV_tel);
        TVGetTime = findViewById(R.id.TV_getTime);
        TVGetCode = findViewById(R.id.TV_getCode);
        TVOk = findViewById(R.id.TV_ok);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Intent intent= getIntent();
        DelivaryMessageBean bean = (DelivaryMessageBean) intent.getSerializableExtra("Message");
        TVTel.setText(bean.getPhone());
        TVGetPosition.setText(bean.getAddress());
        TVGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置随机码
                TVCode.setText(getRandomCode());
                //设置随机时间
                TVGetTime.setText(getData());

            }
        });
        TVOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TVCode.getText().toString().equals("")&&TVGetTime.getText().toString().equals("")){
                    Toast.makeText(GetCode.this, "未生成取件码", Toast.LENGTH_SHORT).show();
                }else{
                    DelivaryMessageBean bean1 = new DelivaryMessageBean(bean.getStoreName(),TVGetPosition.getText().toString(),TVCode.getText().toString(),TVGetTime.getText().toString());
                    Log.d("TAG", "onClick: "+TVGetPosition.getText().toString());
                    Intent intent1 = new Intent();
                    intent1.putExtra("resMes",bean1);
                    setResult(RESULT_OK,intent1);
                    finish();

                }
            }
        });
    }

    //随机获取取件码
    public String getRandomCode(){
        Random random = new Random();
        String code = "0";
        for (int i = 0; i < 9; i++) {
            int a = random.nextInt(9);
            code += String.valueOf(a);
        }
        return code;
    }
    //设置时间
    public String getData(){
        calendar.add(Calendar.MINUTE,30);
        return calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
    }
}